// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'

const demoStore = createSlice({
  name: 'global',
  initialState: { token: '', authRouters: [] as string[] },
  reducers: {
    setToken(store, { payload }) {
      store.token = payload
    },
  },
  // extraReducers(builder) {
  //   builder
  //     .addCase(incrementAsync.pending, state => {
  //       //Promise.pending状态时执行
  //       console.log('🚀 ~ 进行中！')
  //     })
  //     .addCase(incrementAsync.fulfilled, (state, { payload }) => {
  //       //Promise.fulfiled状态时执行，真正修改state的逻辑放此处
  //       console.log('🚀 ~ fulfilled', payload)
  //       state.count += payload
  //     })
  //     .addCase(incrementAsync.rejected, (state, err) => {
  //       //Promise.rejected状态时执行
  //       console.log('🚀 ~ rejected', err)
  //     })
  // },
})
//导出异步方法，第一个参数是命名空间，也是在redux-devtool显示的name，第二个参数是dispatch传入的参数
// export const incrementAsync = createAsyncThunk('global/incrementAsync', async (num: number) => {
//   return await new Promise<number>((resolve, reject) => {
//     setTimeout(() => {
//       resolve(num)
//     }, 2000)
//   })
// })
//导出actions
export const { setToken } = demoStore.actions
//导出reducer给入口文件注册仓库
export default demoStore.reducer
